package com.frxuexi.video

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.adapter.CommentAdapter
import com.frxuexi.lib_base.GlideUtil
import com.frxuexi.lib_base.entity.VideoBeanItem
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.video.databinding.ActivityVideoDetailBinding
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/video/VideoDetailActivity")
@AndroidEntryPoint
class VideoDetailActivity : BaseActivity<ActivityVideoDetailBinding,VideoViewModel>() {
    @Autowired
    lateinit var video:VideoBeanItem
    lateinit var gsy:StandardGSYVideoPlayer
    val commentAdapter by lazy { CommentAdapter() }
    override fun initData() {
        vm.addCommentSuc.observe(this){
            Log.i("===",it.toString())
            commentAdapter.addData(0,it)
            commentAdapter.notifyItemInserted(0)
            binding.rvComment.scrollToPosition(0)
        }

        vm.zanSuc.observe(this){
            binding.videoZan.setImageResource(R.drawable.ic_action_zan)
        }

        vm.noZanSuc.observe(this){
            binding.videoZan.setImageResource(R.drawable.ic_action_nozan)
        }

        vm.getCommentSuc.observe(this){
            commentAdapter.addData(it)
            commentAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        Log.i("===",video.toString())

        gsy=binding.gsy

        gsy.apply {
            backButton.visibility=View.GONE
            setUp(video.videopath,true,"")
            startPlayLogic()
        }

        binding.videoUname.setText(video.authname)
        binding.videoTitle.setText(video.caption)
        GlideUtil.loadRoundImg(video.videopath,binding.videoAuthor)

        binding.rvComment.apply {
            adapter=commentAdapter
            layoutManager=LinearLayoutManager(this@VideoDetailActivity)
        }

        binding.btnComment.setOnClickListener {
            vm.addComment(mapOf("videoId" to video.id,"msg" to binding.videoComment.text.toString()))
        }

        if(video.dianzan==1){
            binding.videoZan.setImageResource(R.drawable.ic_action_zan)
        }else{
            binding.videoZan.setImageResource(R.drawable.ic_action_nozan)
        }

        binding.videoZan.setOnClickListener {
            if(video.dianzan==1){//取消点赞
                vm.noZan(video.id)
            }else{//点赞
                vm.zan(video.id)
            }
        }

        vm.getComments(video.id)
    }

    override fun onPause() {
        super.onPause()
        gsy.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        gsy.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }

}